//
//  SpaceMessage.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/20.
//

#import "SpaceMessage.h"

@implementation SpaceMessage

- (NSString *)getPath {
    return @"space";
}

- (BOOL)methodCallConsumed:(NSString *)method params:(NSDictionary *)params result:(FlutterResult)result
{
    BOOL consumed = YES;

    if ([super methodCallConsumed:method params:params result:result])
        return YES;

    assert(method !=nil);
//    NSLog(@"lengthUnits : %d", method);
    if([method isEqualToString:@"setLengthUnits"]) {
        if (![self isConnected]) {
            result([NSString stringWithFormat:@"%ld", UTEDeviceErrorDisconnect]);
            return YES;
        }
        
        NSNumber *lengthUnits = [params objectForKey:KEY_VALUE];
        NSLog(@"lengthUnits : %@", lengthUnits);
        [[UTEDeviceMgr sharedInstance] setCurrentUnitWeight:([lengthUnits intValue] == 0) block:^(NSInteger errorCode) {
            NSLog(@"setLengthUnits result : %ld", errorCode);
            result([NSString stringWithFormat:@"%ld", errorCode]);
        }];
    } else if([method isEqualToString:@"setWeightUnits"]) {
        if (![self isConnected]) {
            result([NSString stringWithFormat:@"%ld", UTEDeviceErrorDisconnect]);
            return YES;
        }
        
        NSNumber *weightUnits = [params objectForKey:KEY_VALUE];
        NSLog(@"weightUnits : %@", weightUnits);
        [[UTEDeviceMgr sharedInstance] setCurrentUnitWeight:([weightUnits intValue] == 0) block:^(NSInteger errorCode) {
            NSLog(@"setWeightUnits result : %ld", errorCode);
            result([NSString stringWithFormat:@"%ld", errorCode]);
        }];
    } else if ([method isEqualToString:@"setTemperatureUnit"]) {
        if (![self isConnected]) {
            result([NSString stringWithFormat:@"%ld", UTEDeviceErrorDisconnect]);
            return YES;
        }
        
        NSLog(@"setTemperatureUnit");
        NSNumber *temperatureUnit = [params objectForKey:KEY_VALUE];
        [[UTEDeviceMgr sharedInstance] setTemperatureUnit:[temperatureUnit intValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
            NSLog(@"setTemperatureUnit result : %ld", errorCode);
            result([NSString stringWithFormat:@"%ld", errorCode]);
        }];
    } else if ([method isEqualToString:@"resetFactory"]) {
        NSLog(@"resetFactory");
        if (![self isConnected]) {
            result([NSString stringWithFormat:@"%ld", UTEDeviceErrorDisconnect]);
            return YES;
        }
        
        [[UTEDeviceMgr sharedInstance] resetFactory:(0) block:^(NSInteger errorCode, NSDictionary *uteDict) {
            NSLog(@"resetFactory result : %ld", errorCode);
            result([NSString stringWithFormat:@"%ld", errorCode]);
        }];
    } else if ([method isEqualToString:@"setLanguage"]) {
        NSLog(@"setLanguageEnum");
        NSLog(@"setLanguageEnum dictionay : %@", params);
        if (![self isConnected]) {
            result([NSString stringWithFormat:@"%ld", UTEDeviceErrorDisconnect]);
            return YES;
        }
        
        NSNumber *language = [params objectForKey:KEY_LANGUAGE];
        NSNumber *units =  [params objectForKey:KEY_UNITS];

        [[UTEDeviceMgr sharedInstance] setLanguageEnum:[language intValue] unit:[units intValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result([NSString stringWithFormat:@"%ld", errorCode]);
        }];
    } else if ([method isEqualToString:@"setMotionGoal"]) {
        if (![self isConnected]) {
            result([NSString stringWithFormat:@"%ld", UTEDeviceErrorDisconnect]);
            return YES;
        }
        
        NSDictionary *dictionay = [params objectForKey:KEY_VALUE];
        NSNumber *step = [dictionay objectForKey:@"step"];
        NSNumber *calorie =  [dictionay objectForKey:@"calorie"];
        NSNumber *distance =  [dictionay objectForKey:@"distance"];
        NSNumber *duration =  [dictionay objectForKey:@"duration"];
        
        UTEModelSportGoal *goal = [UTEModelSportGoal new];
        goal.goalStep = [step intValue];
        goal.goalCalorie = [calorie intValue];
        goal.goalDistance = [distance intValue];
        goal.goalDuration = [duration intValue];

        [[UTEDeviceMgr sharedInstance] setMotionGoalModel:[NSArray arrayWithObject:goal] block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result([NSString stringWithFormat:@"%ld", errorCode]);
        }];
    }
    else {
        consumed = NO;
    }

    return consumed;
}
@end
